package com.yunji.system.aspectj;


import com.yunji.common.annotation.EventRecord;
import com.yunji.common.base.IEventJbpm;
import com.yunji.common.model.bo.EventRecordSelectBO;
import com.yunji.common.model.domain.EventRecordDO;
import com.yunji.common.model.dto.LoginInfo;
import com.yunji.common.utils.TimeUtils;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.system.context.SystemContext;
import com.yunji.system.dao.IEventRecordDao;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


@Aspect
@Component
@Order(9)
public class EventRecordAspect {

    // 放dao
    @Resource
    private IEventRecordDao eventRecordDao;

    @Pointcut(value = "((execution(* com.yunji.*.dao.*.*(..)) || execution(* com.yunji.*.mapper.*.*(..))) && @annotation(eventRecord))")
    public void daoAndMapperPoint(EventRecord eventRecord) {
    }

    // 应该使用返回通知  @After-returning
    @AfterReturning(value = "daoAndMapperPoint(eventRecord)",argNames = "joinPoint,eventRecord")
    public void afterReturning(JoinPoint joinPoint, EventRecord eventRecord) {
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            if (arg instanceof IEventJbpm) {
                IEventJbpm eventJbpm = (IEventJbpm) arg;
                Long id = eventJbpm.getId();
                Integer newStatus = eventJbpm.getNewStatus();
                String typeName = eventJbpm.getNewStatusName();
                String businessType = eventRecord.businessType();
                EventRecordDO recordDO = new EventRecordDO();
                LoginInfo user = SystemContext.getUser();


                // 如果不唯一 就修改时间
                EventRecordDO select = eventRecordDao.select(EventRecordSelectBO.builder().eventDataId(id).eventStatus(newStatus)
                        .eventType(eventRecord.eventType()).businessType(businessType).build());
                if (BeanUtils.isNotNull(select)){
                    EventRecordDO update = new EventRecordDO();
                    update.setRecordId(select.getRecordId());
                    update.setOperateName(user.getUsername());
                    update.setOperateBelongId(user.getBelongId());
                    update.setOperateBelongName(user.getBelongName());
                    update.setOperateTime(TimeUtils.getNowDate());
                    eventRecordDao.updateById(update);
                }else {
                    recordDO.setOperateId(user.getUserId());
                    recordDO.setEventType(eventRecord.eventType());
                    recordDO.setOperateName(user.getUsername());
                    recordDO.setOperateBelongId(user.getBelongId());
                    recordDO.setOperateBelongName(user.getBelongName());
                    recordDO.setOperateTime(TimeUtils.getNowDate());
                    recordDO.setBusinessType(businessType);
                    recordDO.setEventDataId(id);
                    recordDO.setEventStatus(newStatus);
                    recordDO.setEventStatusName(typeName);
                    eventRecordDao.save(recordDO);
                }
                break;
            }
        }

    }

}
